package com.orhon.smartcampus.modules.schoolaffairs.mutation;

import com.orhon.smartcampus.auth.JwtUtils;
import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.dao.SchoolsDao;
import com.orhon.smartcampus.modules.base.entity.Schools;
import com.orhon.smartcampus.modules.schoolaffairs.InputMapper.SchoolChartSInputMapper;
import com.orhon.smartcampus.modules.schoolaffairs.InputMapper.SchoolOpinionInputMapper;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolChartSayingDao;
import com.orhon.smartcampus.modules.schoolaffairs.dao.SchoolOpinionDao;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolChartSaying;
import com.orhon.smartcampus.modules.schoolaffairs.entity.SchoolOpinion;
import com.orhon.smartcampus.modules.schoolaffairs.gqlInputModel.GQLSchoolChartSayingInput;
import com.orhon.smartcampus.modules.schoolaffairs.gqlInputModel.GQLSchoolOpinionInput;
import com.orhon.smartcampus.modules.schoolaffairs.gqlModel.GQLSchoolChartSaying;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;
import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Component
public class AddSchoolChartSayingMutation implements DataFetcher {

    @Autowired
    SchoolsDao schoolsDao;

    @Autowired
    TokenService tokenService;

    @Autowired
    SchoolChartSayingDao schoolChartSayingDao;

    @Autowired
    UsersDao usersDao;

    @Override
    @Transactional(readOnly = false)
    public Object get(DataFetchingEnvironment environment) throws Exception {
        String token = tokenService.getSystemToken();
        Long schoolId = JwtUtils.getSchoolId(token);
        Long userId = JwtUtils.getUserId(token);
        GQLSchoolChartSayingInput input = SchoolChartSInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));
        SchoolChartSaying schoolChartSaying=null;
        if (input.getId() != null){
            schoolChartSaying = schoolChartSayingDao.get(SchoolChartSaying.class , input.getId());
            schoolChartSaying.setUpdatedAt(new Date());
        }
        else{
            schoolChartSaying = new SchoolChartSaying();
            schoolChartSaying.setCreatedAt(new Date());
        }
        if(input.getContent()!=null){
            schoolChartSaying.setContent(input.getContent());
        }
        if(input.getUrl()!=null){
            schoolChartSaying.setUrl(input.getUrl());
        }
        if (schoolId != null){
            Schools schools = schoolsDao.get(Schools.class ,schoolId);
            schoolChartSaying.setSchoolsBySchool(schools);
        }
        if(userId!=null){
            schoolChartSaying.setPublisher(usersDao.get(Users.class,userId));
        }
        if(input.getMkState()!=null){
            schoolChartSaying.setMkState(input.getMkState());
        }
        try{
            schoolChartSayingDao.saveOrUpdate(schoolChartSaying);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        return Result.ok();
    }
}
